﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using LoadTestReport.DAO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;
using System.Windows.Forms.DataVisualization.Charting;
using System.Collections;
using System.Text.RegularExpressions;
using System.Diagnostics;


namespace LoadTestReport
{
    public class GraphPreparator : System.Windows.Forms.Form
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public GraphPreparator()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();

            // TODO: Add any initialization after the InitForm call

        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            ArrayList countersNcategories = DataReader.getValidCounters(TestParent.selectedRunID);
            ArrayList counters = (ArrayList)countersNcategories[0];
            ArrayList categories = (ArrayList)countersNcategories[1];

            for (int c = 0; c < counters.Count; c++)
            {
                ArrayList machines = DataReader.getMachinesInvolved(TestParent.selectedRunID, counters[c].ToString());                                

                System.Windows.Forms.DataVisualization.Charting.Chart chart1;
                System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
                System.Windows.Forms.DataVisualization.Charting.Title title1 = new System.Windows.Forms.DataVisualization.Charting.Title();

                chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
                ((System.ComponentModel.ISupportInitialize)(chart1)).BeginInit();
                SuspendLayout();
                // 
                // chart1
                // 
                chart1.BackColor = System.Drawing.Color.WhiteSmoke;
                chart1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
                chart1.BackSecondaryColor = System.Drawing.Color.White;
                chart1.BorderlineColor = System.Drawing.Color.FromArgb(((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
                chart1.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
                chart1.BorderlineWidth = 2;
                chart1.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;

                chartArea1.Area3DStyle.Inclination = 15;
                chartArea1.Area3DStyle.IsClustered = true;
                chartArea1.Area3DStyle.IsRightAngleAxes = false;
                chartArea1.Area3DStyle.Perspective = 10;
                chartArea1.Area3DStyle.Rotation = 10;
                chartArea1.Area3DStyle.WallWidth = 0;
                chartArea1.AxisX.LabelAutoFitStyle = ((System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles)(((System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.IncreaseFont | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.DecreaseFont)
                            | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.WordWrap)));
                chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
                chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
                chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
                chartArea1.AxisX.ScrollBar.LineColor = System.Drawing.Color.Black;
                chartArea1.AxisX.ScrollBar.Size = 10;
                chartArea1.AxisX.Minimum = 0;
                chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
                chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
                chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
                chartArea1.AxisY.ScrollBar.LineColor = System.Drawing.Color.Black;
                chartArea1.AxisY.ScrollBar.Size = 10;
                chartArea1.AxisY.Minimum = 0;
                chartArea1.BackColor = System.Drawing.Color.Gainsboro;
                chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
                chartArea1.BackSecondaryColor = System.Drawing.Color.White;
                chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
                chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
                chartArea1.CursorX.IsUserEnabled = true;
                chartArea1.CursorX.IsUserSelectionEnabled = true;
                chartArea1.CursorY.IsUserEnabled = true;
                chartArea1.CursorY.IsUserSelectionEnabled = true;
                chartArea1.Name = "Default";
                chartArea1.ShadowColor = System.Drawing.Color.Transparent;
                chart1.ChartAreas.Add(chartArea1);

                chart1.Location = new System.Drawing.Point(16, 32);
                chart1.Name = "chart1";
                chart1.Size = new System.Drawing.Size(900, 300);
                chart1.TabIndex = 1;


                title1.Alignment = System.Drawing.ContentAlignment.TopCenter;
                title1.Font = new System.Drawing.Font("Trebuchet MS", 16F, System.Drawing.FontStyle.Bold);
                title1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
                title1.Name = "Title1";
                title1.Position.Auto = true;
                title1.Position.Height = 20F;
                title1.Position.Width = 80F;
                title1.ShadowColor = System.Drawing.Color.FromArgb(((int)(((byte)(32)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
                title1.ShadowOffset = 3;
                title1.Text = counters[c].ToString();

                chart1.Titles.Add(title1);

                for (int i = 0; i < machines.Count; i++)
                {
                    System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
                    System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();

                    legend1.BackColor = System.Drawing.Color.Transparent;
                    legend1.Enabled = true;
                    legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
                    legend1.IsTextAutoFit = false;
                    legend1.Name = (String)machines[i];
                    legend1.Title = "";

                    series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
                    series1.BorderWidth = 3;
                    series1.ChartArea = "Default";
                    series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;

                    String[] myColors = { "cadetblue", "chartreuse", "chocolate", "coral", "cornflowerblue", "cyan", "darkblue", "darkcyan", "darkgoldenrod", "darkgray", "darkgreen", "darkkhaki", "darkmagenta", "darkolivegreen", "darkorange", "darkorchid", "darkred", "darksalmon", "darkseagreen", "darkslateblue", "darkslategray", "darkturquoise", "darkviolet", "deeppink", "dimgray", "dodgerblue", "firebrick", "magenta", "maroon", "mediumaquamarine", "mediumblue", "mediumorchid", "mediumpurple", "mediumseagreen", "mediumslateblue" };

                    System.Drawing.Color randomColor = System.Drawing.Color.FromName(myColors[i]);
                    series1.Color = randomColor;
                    //series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));

                    series1.MarkerSize = 1;
                    series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
                    series1.Name = (String)machines[i];
                    series1.ShadowColor = System.Drawing.Color.Black;
                    series1.ShadowOffset = 0;
                    series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
                    series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;

                    //if (temp.Contains("Available MBytes"))
                    //{ }

                    String Category_Counter = categories[c].ToString() + "@" + counters[c].ToString();
                    bool shouldCreate = false;
                    try
                    {
                        if(TestParent.counterState.ContainsKey(Category_Counter))
                        {
                            shouldCreate = TestParent.counterState[Category_Counter];
                        }                        
                    }
                    catch { }
                    // Populate series data    
                    if (shouldCreate)
                    {
                        ArrayList data = DataReader.getGraphData((String)machines[i], TestParent.selectedRunID, counters[c].ToString(), categories[c].ToString());

                        if (data == null) continue;

                        Array xList = ((ArrayList)data[0]).ToArray(typeof(Double));
                        Array yList = ((ArrayList)data[1]).ToArray(typeof(Double));

                        if (xList.Length == 0 || xList.Length == 0) continue;

                        chart1.Legends.Add(legend1);
                        chart1.Series.Add(series1);

                        chart1.Series[(String)machines[i]].Points.DataBindXY(xList, yList);
                    }
                }

                Controls.Add(chart1);
                Font = new System.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                Name = "LineCurvesChartType";
                Size = new System.Drawing.Size(900, 400);
                //Load += new System.EventHandler(LineCurvesChartType_Load);
                ((System.ComponentModel.ISupportInitialize)(chart1)).EndInit();
                ResumeLayout(false);
                BackColor = System.Drawing.Color.White;

                String temp = counters[c].ToString();
                temp = temp.Replace("#","Number");
                String fileName = Regex.Replace(temp, "[^0-9a-zA-Z]+", "-");

                double progress = (double)70 * (double)((double)c / (double)(counters.Count));

                TestParent.ProgressPercentage = 20 + (int)progress;

                if (chart1.Series.Count == 0) continue;

                try
                {
                    using (FileStream fs = new FileStream("C:\\Templates\\Images\\Graphs\\" + fileName + ".jpg", FileMode.Create))
                    {
                        chart1.SaveImage(fs, ChartImageFormat.Jpeg);
                    }
                }
                catch (Exception ex) { }               
            }
        }
        #endregion              
    }
}
